﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Xml;

namespace Wild2.Models.HelpSystem
{
    public class HelpProcessor
    {
        /// <summary>
        /// Возвращает класс с данными справки, контент профильтрован и в фильтрации не нуждается
        /// </summary>
        /// <returns></returns>
        public static Types.Help GetHelp()
        {
            Types.Help help = new Types.Help();
            using (TextReader textReader = new StreamReader(HttpContext.Current.Request.MapPath("~/Content/terms.xml")))
            {
                using (XmlReader reader = XmlReader.Create(textReader))
                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            if (reader.Name.ToLower() == "root")
                            {
                                //Заголовок справки
                                help.Caption = HttpUtility.HtmlEncode(reader.GetAttribute("name"));
                                //Читаем сабноды секции
                                using (XmlReader sectionReader = reader.ReadSubtree())
                                {
                                    help.Razds = new List<Types.HelpRazd>();

                                    while (sectionReader.Read())
                                    {
                                        if (sectionReader.NodeType == XmlNodeType.Element)
                                        {
                                            if (sectionReader.Name.ToLower() == "section")
                                            {
                                                Types.HelpRazd razd = new Types.HelpRazd();
                                                razd.IdRazd = Convert.ToInt32(sectionReader.GetAttribute("id"));
                                                razd.Name = HttpUtility.HtmlEncode(sectionReader.GetAttribute("name"));
                                                using (XmlReader nodeReader = reader.ReadSubtree())
                                                {
                                                    razd.Nodes = new List<Types.HelpNode>();

                                                    while (nodeReader.Read())
                                                    {
                                                        if (nodeReader.NodeType == XmlNodeType.Element)
                                                        {
                                                            if (nodeReader.Name.ToLower() == "article")
                                                            {
                                                                Types.HelpNode node = new Types.HelpNode();
                                                                node.IdNode = Convert.ToInt32(nodeReader.GetAttribute("id"));
                                                                node.Name = HttpUtility.HtmlEncode(nodeReader.GetAttribute("name"));
                                                                node.Text = new Dictionary<int,string>();
                                                                nodeReader.Read();
                                                                string[] elements = nodeReader.Value.Split(new[] { "|||" }, StringSplitOptions.RemoveEmptyEntries);
                                                                for (int i = 0; i < elements.Length; i++)
                                                                {
                                                                    elements[i] = elements[i].Trim(new[] { ' ', '\t', '\n', '\r' });
                                                                    node.Text.Add(i, Text.Helper.Bbcode(Text.Helper.nbsp(HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(elements[i]))), true));
                                                                }
                                                                razd.Nodes.Add(node);
                                                            }
                                                        }
                                                    }
                                                }
                                                help.Razds.Add(razd);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return help;
        }
    }
}